/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author GiangLT
 */
@Entity
@Table(name = "OMAP_ROLE", catalog = "")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "OmapRole.findAll", query = "SELECT o FROM OmapRole o"),
    @NamedQuery(name = "OmapRole.findByRoleId", query = "SELECT o FROM OmapRole o WHERE o.roleId = :roleId"),
    @NamedQuery(name = "OmapRole.findByRoleName", query = "SELECT o FROM OmapRole o WHERE o.roleName = :roleName"),
    @NamedQuery(name = "OmapRole.findByDescription", query = "SELECT o FROM OmapRole o WHERE o.description = :description")})
public class OmapRole implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ROLE_ID")
    private Integer roleId;
    @Size(max = 100)
    @Column(name = "ROLE_NAME")
    private String roleName;
    @Size(max = 400)
    @Column(name = "DESCRIPTION")
    private String description;
    @JoinTable(name = "ROLE_PERMISSION", joinColumns = {
        @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "PERMISSION_ID", referencedColumnName = "ID")})
    @ManyToMany
    private List<Permission> permissionList;
    @JoinTable(name = "OMAP_USER_ROLE", joinColumns = {
        @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
    @ManyToMany
    private List<OmapUser> omapUserList;

    public OmapRole() {
    }

    public OmapRole(Integer roleId) {
        this.roleId = roleId;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @XmlTransient
    public List<Permission> getPermissionList() {
        return permissionList;
    }

    public void setPermissionList(List<Permission> permissionList) {
        this.permissionList = permissionList;
    }

    @XmlTransient
    public List<OmapUser> getOmapUserList() {
        return omapUserList;
    }

    public void setOmapUserList(List<OmapUser> omapUserList) {
        this.omapUserList = omapUserList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (roleId != null ? roleId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof OmapRole)) {
            return false;
        }
        OmapRole other = (OmapRole) object;
        if ((this.roleId == null && other.roleId != null) || (this.roleId != null && !this.roleId.equals(other.roleId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.thienloc.omap.entity.OmapRole[ roleId=" + roleId + " ]";
    }

    public String getUserListString() {
        if (omapUserList != null && !omapUserList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (OmapUser omapUser : omapUserList) {
                sb.append(omapUser.getUsername());
                sb.append(", ");
            }
            sb.delete(sb.lastIndexOf(", "), sb.length());
            return sb.toString();
        } else {
            return "";
        }
    }
}
